/**
 * Created by Demon-pro on 2015/7/27.
 */


var Page = function (){
    function initReadonly(){
        $("#E6,#E5,#E7").prop("readonly",true);
    }



    var getUpdate = function(){
        $("input").on("blur",function(){
            update($(this));
        })
    }

    function selectHandle(){
        $("#E4").on("change",function (){
            initReadonly();
            switch (parseInt($(this).val())){
                case 1:
                    $("#E7").prop("readonly",false);
                    update($(this));
                    break;
                case 2:
                    $("#E6,#E5").prop("readonly",false);
                    update($(this));
                    break;
            }
        }).change();
    }

    function e237Handle(){
        $("#E237").on("change",function (){
            update($(this));
        }).change();
    }

    var update = function ($obj) {
        validate.init($obj)
        if (validate.isNotNumber($obj.val())) {
            validate.invalid();
            return;
        }
        $.ajax({
            url: basePath + "tools/header/update",
            type:"post",
            dataType:"json",
            data:{
                paramName:$obj.attr("id"),
                paramValue:$obj.val()
            }
        });
        validate.valid()
    }

    function calculateHandle() {

        $("#calE15").click(function () {
            $.get(basePath + "tools/header/calE15", function (data) {
                $("#E15").val(data);
            })
        });

        $("#cal").click(function () {
            Metronic.blockUI({
                boxed: true,
                message: "计算中"
            });
            $.post(basePath + "tools/header/calAll", function (data) {
                $("input").autoInject({
                    data: data,
                    mode: "id"
                });
                Metronic.unblockUI();
            })
        })
    }

    return{
        init:function() {
            update($("#E12"));
            update($("#E9"));
            getUpdate();
            selectHandle();
            e237Handle();
            calculateHandle();
            window.onunload = function(){
                $.post(basePath + "tools/header/clean",function(){});
            };
        }
    }
}()